package com.example.anclient;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.SplittableRandom;

public class MainActivity extends AppCompatActivity {
    /* 网络设备 */
    private ANApplicationNetwork network;
    private final String serverAddress = "47.109.94.67";
    private final int serverPort = 2933;
    private TextView titleLabel;
    private TextView onChargingLabel;
    private TextView chargingTimeVariantLabel;
    private TextView chargeProgressPercent;
    private ProgressBar chargeProgressBar;
    private TextView temperatureLabel;
    private TextView powerVariantLabel;
    private TextView voltageVariantLabel;
    private TextView balanceVariantLabel;
    private TextView priceVariantLabel;
    private TextView remainingElectricityLabel;
    private TextView estimateFullTimeLabel;
    private Socket   socket;

    public void setErrorMessageTitle(String string) {
        titleLabel.setText(string);
    }

    public void setChargeProgress(float percent) {
        String string = String.format(Locale.getDefault(), "%.1f%%", percent * 100.0);
        runOnUiThread(() -> {
            chargeProgressBar.setProgress(Float.valueOf((float) (percent * 100.0)).intValue());
            chargeProgressPercent.setText(string);
        });
    }

    public void setChargePower(float chargePower) {
        String string = String.format(Locale.getDefault(), "%.1f kW", chargePower);
        runOnUiThread(() -> powerVariantLabel.setText(string));
    }

    public void setChargeVoltage(float chargeVoltage) {
        String string = String.format(Locale.getDefault(), "%.1f V", chargeVoltage);
        runOnUiThread(() -> voltageVariantLabel.setText(string));
    }

    public void setChargeBalance(float chargeBalance) {
        String string = String.format(Locale.getDefault(), "%.2f 元", chargeBalance);
        runOnUiThread(() -> balanceVariantLabel.setText(string));
    }

    public void setCurrentPrice(float currentPrice) {
        String string = String.format(Locale.getDefault(), "%.2f元/kWh", currentPrice);
        runOnUiThread(() -> priceVariantLabel.setText(string));
    }

    public void setRemainingElectricity(float remainingElectricity) {
        String string = String.format(Locale.getDefault(), "%.1f kWh", remainingElectricity);
        runOnUiThread(() -> remainingElectricityLabel.setText(string));
    }

    public void setEstimateFullTime(int hour, int minute, int second) {
        String string = String.format(Locale.getDefault(), "%02d:%02d:%02d", hour, minute, second);
        runOnUiThread(() -> estimateFullTimeLabel.setText(string));
    }

    public void setTemperature(float temperature) {
        String string = String.format(Locale.getDefault(), "%.1f ℃", temperature);
        runOnUiThread(() -> temperatureLabel.setText(string));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        titleLabel = findViewById(R.id.titleLabel);
        Button fastModeButton = findViewById(R.id.fastModeButton);
        Button slowModeButton = findViewById(R.id.slowModeButton);
        Button autoModeButton = findViewById(R.id.autoModeButton);
        Button handModeButton = findViewById(R.id.handModeButton);
        Button stopModeButton = findViewById(R.id.stopModeButton);
        onChargingLabel = findViewById(R.id.onChargingLabel);
        chargingTimeVariantLabel = findViewById(R.id.chargingTimeVariantLabel);
        chargeProgressPercent = findViewById(R.id.chargeProgressPercent);
        chargeProgressBar = findViewById(R.id.chargeProgressBar);
        temperatureLabel = findViewById(R.id.temperatureLabel);
        powerVariantLabel = findViewById(R.id.powerVariantLabel);
        voltageVariantLabel = findViewById(R.id.voltageVariantLabel);
        balanceVariantLabel = findViewById(R.id.balanceVariantLabel);
        priceVariantLabel = findViewById(R.id.priceVariantLabel);
        remainingElectricityLabel = findViewById(R.id.remainingElectricityLabel);
        estimateFullTimeLabel = findViewById(R.id.estimateFullTimeLabel);

        fastModeButton.setOnClickListener(v -> network.sendControlMessage("m=f"));
        slowModeButton.setOnClickListener(v -> network.sendControlMessage("m=s"));
        autoModeButton.setOnClickListener(v -> network.sendControlMessage("m=a"));
        handModeButton.setOnClickListener(v -> network.sendControlMessage("m=h"));
        stopModeButton.setOnClickListener(v -> network.sendControlMessage("m=n"));

        network = new ANApplicationNetwork(this, "192.168.100.6");
        network.startListening();
        network.registerToServer();
        network.subscribeDevice("esp01s-0016");
    }
}